package com.dd.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dd.entity.DdUser;
import com.dd.entity.DdUserpage;
import com.dd.util.PageList;

/**
 * A data access object (DAO) providing persistence and search support for
 * DdUserpage entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.dd.entity.DdUserpage
 * @author MyEclipse Persistence Tools
 */

public class DdUserpageDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(DdUserpageDAO.class);
	// property constants
	public static final String USERID = "userid";
	public static final String SHOPID = "shopid";

	protected void initDao() {
		// do nothing
	}

	public void saveOrUpdate(DdUserpage transientInstance) {
		log.debug("saving DdUserpage instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(DdUserpage persistentInstance) {
		log.debug("deleting DdUserpage instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public DdUserpage findById(java.lang.Integer id) {
		log.debug("getting DdUserpage instance with id: " + id);
		try {
			DdUserpage instance = (DdUserpage) getHibernateTemplate().get(
					"com.dd.entity.DdUserpage", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding DdUserpage instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from DdUserpage as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public PageList findByUserid(int userid,int skip,int count) {
		try {
			PageList pl = new PageList();

			String maxsizeStr = ("select count(*) from DdUserpage where userid="+userid);
			Connection conn = this.getSession().connection();
			Statement stat = conn.createStatement();
			ResultSet rs = stat.executeQuery(maxsizeStr);
			int max = 0;
			while (rs.next()) {
				max = rs.getInt(1);
			}
			Query q = this.getSession().createQuery("from DdUserpage where userid="+userid+" order by pageindex,id desc");
			q.setFirstResult(skip);
			q.setMaxResults(count);
			List list = q.list();
			pl.addAll(list);
			pl.setTotalCount(max);			
			return pl;	
		} catch (Exception re) {
			re.printStackTrace();
			return new PageList();
		}

	}
	public int findMaxIndex(int userid) {
		try {
			String queryString = "from DdUserpage where userid= ? order by pageindex,id desc";
			List list =  getHibernateTemplate().find(queryString, userid);
			if(list.size()==0){
				return 100;
			}else{
				DdUserpage dup = (DdUserpage)list.get(0);
				return (dup.getPageindex()+100);
			}
		} catch (RuntimeException re) {
			log.error("findByUserid failed", re);
			throw re;
		}

	}
	public List findByShopid(int shopid) {
		try {
			String queryString = "from DdUserpage where shopid= ? order by id desc";
			return getHibernateTemplate().find(queryString, shopid);
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}
	}

	public boolean isExist(int userid,int shopid) {
		try {
			String queryString = "from DdUserpage where shopid= "+shopid+" and userid ="+userid+"";
			List li =  getHibernateTemplate().find(queryString);
			if(li.size()==0){
				return false;
			}else{
				return true;
			}
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}
	}
	public DdUserpage findByUserAndShopid(int userid,int shopid) {
		try {
			String queryString = "from DdUserpage where shopid= "+shopid+" and userid ="+userid+"";
			List li =  getHibernateTemplate().find(queryString);
			if(li.size()==0){
				return null;
			}else{
				return (DdUserpage)li.get(0);
			}
		} catch (RuntimeException re) {
			log.error("findByShopid failed", re);
			throw re;
		}
	}
	
}